package com.pangu.web.view;

import java.util.Locale;

import org.nutz.ioc.Ioc;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.View;
import org.nutz.mvc.ViewMaker;

public abstract class ViewMakerProxy implements ViewMaker {
	protected static final Log logger = Logs.getLog(ViewMakerProxy.class);
	
	private Locale locale = Locale.getDefault();

	protected abstract String getViewResolverName();
	
	protected abstract String getViewName();
	
	public View make(Ioc ioc, String type, String value) {
		ViewResolver resolver = ioc.get(ViewResolver.class, getViewResolverName());
		if (resolver == null)
			return null;
		String viewname = getViewName();
		if (viewname != null && viewname.equalsIgnoreCase(type)) {
			try {
				return resolver.resolveViewName(value, getLocale());
			} catch (Exception e) {
				logger.error("Can't resolve view: " + type + "," + value, e);
				return null;
			}
		}
		return null;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}
	
	public Locale getLocale() {
		return this.locale;
	}
}
